﻿using BussinessObject.ObjectOne;
using BussinessObject.ObjectTwo;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Class_Entity.ObjectTwo;
using System.IO;
using ProjectHK3.App_Code;
namespace ProjectHK3.Object_One
{
    public partial class AddNewAccount : System.Web.UI.Page
    {
        B_Branches ex = new B_Branches();
        B_GetAccount fx = new B_GetAccount();
        Account sx = new Account();
        DataTable tb = new DataTable();
        public static bool anh_Status;
        public static String anh_fie;
        public static String tam = "~/Object One/Pic/Ava/em.jpg";
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                LoadAllData();
                anh_Status = false;
                Literal2.Text = "Profile is waiting for updates";
            }
        }
        public bool checkPin(String pin)
        {
            DataTable tg = new DataTable();
            tg = fx.getPin(pin);
            if (tg.Rows.Count > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public void LoadAllData()
        {
            DropDownList1.DataSource = ex.LoadDropbox();
            DropDownList1.DataTextField = "CITY_NAME";
            DropDownList1.DataValueField = "CITY_ID";
            DropDownList1.DataBind();

            tb = ex.LoadContent(int.Parse(DropDownList1.SelectedValue));
            DropDownList6.DataSource = tb;
            DropDownList6.DataTextField = "BRANCH_NAME";
            DropDownList6.DataValueField = "BRANCH_ID";
            DropDownList6.DataBind();
        }
        public void uploadPic()
        {
            String[] valids = { "bmp", "gif", "PNG", "jpg", "jpeg", "png", "BMP", "GIF", "JPEG", "JPG", "PNG" };
            bool isvalids = false;
            if (FileUpload2.HasFile)
            {
                try
                {
                    String txx = Path.GetExtension(FileUpload2.PostedFile.FileName);

                    for (int i = 0; i < valids.Length; i++)
                    {
                        if (txx == "." + valids[i])
                        {
                            isvalids = true;
                            break;
                        }
                    }
                    if (isvalids == true)
                    {
                        if (FileUpload2.PostedFile.ContentLength < 9002400)
                        {
                            String filename = Path.GetFileName(FileUpload2.FileName);
                            anh_fie = "~/Object One/Pic/Ava/" + filename;
                            FileUpload2.SaveAs(Server.MapPath("/Object One/Pic/Ava/") + filename);
                            anh_Status = true;
                        }
                        else
                            Literal1.Text = "upload Status: the file has to be less than";
                    }
                    else
                        Literal1.Text = "Only image";
                }
                catch (Exception ex)
                {
                    Literal1.Text = "cannot up load";
                }
            }
        }
        public void Insert(String anh,int status)
        {
            sx.USER_NAME = TextBox1.Text.ToString().Trim();
            sx.PASSWORD = TextBox2.Text.ToString();
            sx.FIRST_NAME = TextBox6.Text.ToString().Trim();
            sx.LAST_NAME = TextBox7.Text.ToString().Trim();
            sx.EMAIL = TextBox4.Text.ToString().Trim();
            sx.PHONE_NUMBER = TextBox5.Text.ToString().Trim();
            sx.BIRTH_DATE = TextBox8.Text.ToString();
            sx.CITY_ID = int.Parse(DropDownList1.SelectedValue.ToString());
            sx.CREATE_DATE = DateTime.Now;
            sx.DESCRIPTION = TextBox11.Text.ToString();
            sx.ADDRESS = TextBox9.Text.ToString();
            sx.ROLE = int.Parse(DropDownList3.SelectedValue.ToString());
            sx.POSITION = DropDownList4.SelectedValue.ToString();
            sx.PIN_CODE = TextBox10.Text.ToString();
            sx.ACTIVE =int.Parse(DropDownList5.SelectedValue.ToString());
            sx.PICTURE = anh;
            sx.STATUS = status;
            sx.BRANCH_ID = int.Parse(DropDownList6.SelectedValue.ToString());
            if (fx.InsertNewAccount(sx) == true)
            {
                Alert.Show("Insert Seccessful!!!");
            }
            else
            {
                Alert.Show("Fail");
            }

        }
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            tb = fx.getvalAccount(TextBox1.Text);
            if (tb.Rows.Count > 0)
            {
                Label19.Text = "Account already exists";
                TextBox1.Text = "";
                TextBox1.Focus();
            }
            else
            {
                Label19.Text = "You can use this account";
                TextBox1.Focus();
                Label19.Visible = false;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
            TextBox6.Text = "";
            TextBox7.Text = "";
            TextBox8.Text = "";
            TextBox9.Text = "";
            TextBox10.Text = "";
            TextBox11.Text = "";
            TextBox1.Focus();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int satus = 0;
            if (checkPin(TextBox10.Text.ToString()) == false)
            {
                Alert.Show("Pin Code already exist!!!");
                TextBox10.Text = "";
                TextBox10.Focus();
            }else{
            if(CheckBox1.Checked == true)
            {
                satus=1;
            }else
            {
                satus=0;
            }
            tb = fx.getvalAccount(TextBox1.Text);
            if (tb.Rows.Count > 0)
            {
                Label19.Text = "Account already exists";
                TextBox1.Focus();
            }
            else
            {
                uploadPic();
                String anh_tam = "";
                if (anh_Status == false)
                {
                    anh_tam = tam;
                    Insert(anh_tam, satus);
                }
                else
                {
                    anh_tam = anh_fie;
                    Insert(anh_tam, satus);
                }
                }
            }
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            Label9.Visible = false;
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox1.Checked == true)
            {
                Literal2.Text = "Add profile successful";                
            }
            else
            {
                Literal2.Text = "Profile is waiting for updates";
            }
        }

        protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue.ToString() == "0")
            {
                CheckBox1.Checked = false;
            }
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            if (checkPin(TextBox10.Text.ToString()) == false)
            {
                Alert.Show("Pin Code already exist!!!");
                TextBox10.Text = "";
                TextBox10.Focus();
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            DropDownList6.Items.Clear();
            tb = ex.LoadContent(int.Parse(DropDownList1.SelectedValue));
            DropDownList6.DataSource = tb;
            DropDownList6.DataTextField = "BRANCH_NAME";
            DropDownList6.DataValueField = "BRANCH_ID";
            DropDownList6.DataBind();
        }

        protected void TextBox2_PreRender(object sender, EventArgs e)
        {
            TextBox2.Attributes.Add("value", TextBox2.Text);
            base.OnPreRender(e);
        }

        protected void TextBox3_PreRender(object sender, EventArgs e)
        {
            TextBox3.Attributes.Add("value", TextBox3.Text);
            base.OnPreRender(e);
        }

        protected void FileUpload2_PreRender(object sender, EventArgs e)
        {
            
        }
    }
}